var png_hero, png_tris, png_hash; //image handles
var png_star;
var snd_bgscore; //bgmusic handle
var snd_up, snd_down; //pick up sfx
var canvas;
var ctx;
var g_mx=300, g_my=200; //global variables to hold the mouse posx and posy
var g_size = 32; //handles the scaling of images.
var g_player; //variable to hold player object
var g_enemyList = new Array(); 
var g_particleList = new Array(); 
var g_startGame = false;
var g_mesg;
var g_pausebutton;
var g_numHighScores = 5;

function onMouseMove(e){
	var posx = 0;
	var posy = 0;

    if (e.pageX || e.pageY){
        posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY){
        posx = e.clientX + document.body.scrollLeft
            + document.documentElement.scrollLeft;
        posy = e.clientY + document.body.scrollTop
            + document.documentElement.scrollTop;
    }
    // posx and posy contain the mouse position relative to the document
    // Do something with this information
    g_mx = (posx-canvas.offsetLeft); 
    g_my = (posy-canvas.offsetTop);
}

function GameLoop()
{
    update();
    draw();
}

function StartGame()
{
    snd_bgscore.play(); 
    g_startGame=true;
    g_mesg.style.visibility="hidden";
    g_pausebutton.style.visibility="visible";
}


function LoadingDone()
{
    //all are loaded get rid of the loading gif
    var spinner = document.getElementById("gif_spinner");
    spinner.style.visibility="hidden";
    g_mesg = document.getElementById("mesg");
    g_mesg.innerHTML="<input style='height:30;' type=button value='Start' onClick='StartGame();'>";
    g_mesg.style.visibility="visible";
    g_pausebutton = document.getElementById("button_pause");
    //start the game related init.
    png_hero = document.getElementById("png_hero");
    png_tris = document.getElementById("png_tris");
    png_hash = document.getElementById("png_hash");
    png_star = document.getElementById("png_star");
    snd_bgscore = document.getElementById("snd_bgscore");
    snd_up = document.getElementById("snd_up");
    snd_down = document.getElementById("snd_down");
    snd_bgscore.addEventListener('ended', function() {
        this.currentTime = 0;
        this.play();
    }, false);
    
    canvas = document.getElementById("canv");
    canvas.style.visibility="visible";
    canvas.onmousemove=onMouseMove;
    ctx = canvas.getContext('2d');
    //load the previous high scores
    var hs = window.localStorage["highscores"];
    var hsn = window.localStorage["hsnames"];
    if(hs && hsn){//we are loading previous data
        g_highScores = hs.split(',');
        g_hsNames = hsn.split(',');
            for(var i in g_highScores){
                g_highScores[i] = parseInt(g_highScores[i]);
            }
    }else{  //we are running for the first time.
        for(var i = 0; i < g_numHighScores; i++){
            g_highScores.push(0);
            g_hsNames.push('---');
        }
    }
    //---done..
    //create the player object
    g_player = new Player();
    g_TextMessage = new TextMessage("gmesg");
    //enemy object
    g_enemyList.push(new Enemy(0, 0, png_tris, FollowPlayer));
    var FPS = 30.0;
    g_time = 0;
    var d = new Date();
    g_startTime = d.getTime();
    setInterval(GameLoop, 1000/FPS);
}
